package org.geek.week1.homework2;

import org.apache.commons.io.IOUtils;
import org.geek.week1.Util;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * @author wangnan
 * @create 2021/9/18
 */
public class ChangeHelloFile {
    /**
     *替换文件 Hello.xlass.zip文件 ，打印内容：Hello, classLoader!
     */
    private static void replaceToOriginalFile(){
        ChangeHelloFile changeHelloFile = new ChangeHelloFile();
        final URL resourcePath = changeHelloFile.getClass().getResource("/");
        final URL changeFile = changeHelloFile.getClass().getResource("/hello-file/original/Hello.xlass.zip");
        final String resourceFilePath = Util.textDecode(resourcePath.getFile())+ "/Hello.xlass.zip";
        try {
            IOUtils.copy(changeFile, new File(resourceFilePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 替换文件 Hello.xlass.zip文件 ，打印内容：hello change
     */
    private static void replaceToChangeFile(){
        ChangeHelloFile changeHelloFile = new ChangeHelloFile();
        final URL resourcePath = changeHelloFile.getClass().getResource("/");
        final URL changeFile = changeHelloFile.getClass().getResource("/hello-file/change/Hello.xlass.zip");
        final String resourceFilePath = Util.textDecode(resourcePath.getFile())+ "/Hello.xlass.zip";
        try {
            IOUtils.copy(changeFile, new File(resourceFilePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
       replaceToOriginalFile();
       //replaceToChangeFile();
    }
}
